﻿using System;
using System.Data;
using System.Linq;
using System.Threading;

namespace PLinq
{
    class Program
    {
        static void Main(string[] args)
        {
            //并行就是  分区，然后对分区合并
           var list= Enumerable.Range(0, 100);

            // AsOrdered  将并行结果按原来的顺序排序
            var d = from a in list
                   // .AsParallel() //AsParallel  将串行代码转成并行
                   // .AsOrdered() // AsOrdered  将并行结果按原来的顺序排序
                   // .AsParallel().AsSequential()  //AsSequential 将并行转换串行
                   //WithDegreeOfParallelism 设置当前处理器数量， WithCancellation  取消
                   //WithExecutionMode( ParallelExecutionMode.ForceParallelism)  强制并行
                   .AsParallel().WithDegreeOfParallelism(Environment.ProcessorCount).WithExecutionMode( ParallelExecutionMode.ForceParallelism)// .WithCancellation


                    select new { t = Thread.CurrentThread.ManagedThreadId, a };
            foreach (var item in d)
            {
                Console.WriteLine($"tid={item.t} ,n={item.a}");
            }


           


            Console.Read(); 
        }
    }
}
